package city.spring.controller.mealService;


import city.spring.modules.bed.entity.BedRoomEntity;
import city.spring.modules.bed.service.BedRoomService;
import city.spring.utils.MyBatisUtils;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 通过送餐区域查找房间号的控制器
 *
 * @author yzh
 */
@RestController
@RequestMapping("/send-area/{sendId}/room")
public class BedRoomAreaController extends ApiController {

    /**
     * <strong></strong> Service
     */
    private final BedRoomService bedRoomService;
    /**
     * 实体类（）可排序字段
     */
    private final List<SFunction<BedRoomEntity, ?>> entityOrderFields;

    public BedRoomAreaController(BedRoomService bedRoomService) {
        this.bedRoomService = bedRoomService;
        entityOrderFields = new ArrayList<>();
        entityOrderFields.add(BedRoomEntity::getId);
        entityOrderFields.add(BedRoomEntity::getBedFloorId);
        entityOrderFields.add(BedRoomEntity::getCode);
        entityOrderFields.add(BedRoomEntity::getName);
        entityOrderFields.add(BedRoomEntity::getDirection);
        entityOrderFields.add(BedRoomEntity::getGmtCreate);
        entityOrderFields.add(BedRoomEntity::getGmtModified);
    }

    /**
     * 获取全部的 <strong></strong> 列表
     */
    @GetMapping("all")
    public Object listAll(@PageableDefault(sort = {"gmtCreate"}) Pageable pageable, BedRoomEntity entity) {
        LambdaQueryChainWrapper<BedRoomEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        return success(lambdaQuery.list());
    }

    /**
     * 分页获取 <strong></strong> 列表
     *
     * @param pageable 分页参数信息
     */
    @GetMapping
    public Object list(@PageableDefault(sort = {"gmtCreate"}) Pageable pageable, BedRoomEntity entity, @PathVariable String sendId) {
        LambdaQueryChainWrapper<BedRoomEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        lambdaQuery.eq(BedRoomEntity::getAreaId, sendId);
        Page<BedRoomEntity> page = lambdaQuery.page(MyBatisUtils.toPage(pageable, false));
        return success(page);
    }

    /**
     * 获取一个 <strong></strong>
     *
     * @param id 主键ID
     */
    @GetMapping("{id}")
    public Object info(@PathVariable String id) {
        return success(bedRoomService.getById(id));
    }

    /**
     * 添加一个 <strong></strong>
     *
     * @param entity 新增的信息
     */
    @PostMapping
    public Object add(@RequestBody BedRoomEntity entity) {
        bedRoomService.save(entity);
        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.LOCATION, entity.getId());
        return new ResponseEntity<>(headers, HttpStatus.CREATED);
    }

//    @PutMapping("{id}")
//    public Object update(@PathVariable String id, @RequestBody BedRoomEntity entity) {
//        bedRoomService.lambdaUpdate()
//                .set(BedRoomEntity::getId, entity.getId())
//                .eq(BedRoomEntity::getId, id)
//                .update();
//        return new ResponseEntity<>(HttpStatus.OK);
//    }

    /**
     * 删除一个 <strong></strong>
     *
     * @param id 主键ID
     */
    @DeleteMapping("{id}")
    public Object delete(@PathVariable String id) {
        bedRoomService.removeById(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 删除多个 <strong></strong>
     *
     * @param ids 主键ID列表
     */
    @DeleteMapping
    public Object deleteIds(@RequestBody List<String> ids) {
        bedRoomService.removeByIds(ids);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 删除房间的关联区域id
     *
     * @param
     */
    @PutMapping("{id}")
    public Object deleteSendId(@PathVariable String id) {
        bedRoomService.deleteSendId(id);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象： <strong></strong>
     * @return lambda query chain wrapper
     */
    private LambdaQueryChainWrapper<BedRoomEntity> buildLambdaQuery(BedRoomEntity entity) {
        LambdaQueryChainWrapper<BedRoomEntity> lambdaQuery = bedRoomService.lambdaQuery();
        lambdaQuery.eq(StringUtils.isNotBlank(entity.getId()), BedRoomEntity::getId, entity.getId());
        return lambdaQuery;
    }

}
